<?php
namespace app\admin\model;

use app\admin\exception\DataNotFoundException;
use app\admin\exception\LogicException;
use app\common\event\UserSubscribe;
use think\facade\Event;
use think\model\concern\SoftDelete;

class User extends AdminModel
{
    use SoftDelete;
	protected $name = 'system_user';
	protected $autoWriteTimestamp = true;
	protected $createTime = 'createtime';
	protected $updateTime = 'updatetime';
	protected $defaultSoftDelete = null;
	protected $deleteTime = 'deletetime';
	/**
	 * 新增、更新需要操作的字段
	 */
	protected $submitField = [
		'status' => 'int',
		'passwd' => 'string',
	];



	public function role(){
		return $this->belongsToMany(Role::class,UserRole::class,'role_id','admin_id');
	}

	public function roles(){
		return $this->hasManyThrough(Role::class,UserRole::class,'admin_id','id','id');
	}

	protected function setAttrStatus($data){
		if(isset($data['status'])) return 1;
		return 0;
	}

	protected function setAttrPasswd($data){
		if(empty($data['passwd'])){
			unset($this->submitField['passwd']);
		}else{
			return password_hash($data['passwd'],PASSWORD_DEFAULT);
		}
	}

	protected function getAvatarAttr($value){
		return get_file_path($value);
	}

	public function getLastLoginTimeAttr($value){
	    return date('Y-m-d H:i:s',$value);
    }
}

